<template>
  <div class="container">
    <div class="btnList">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="erpCenter.code"
        placeholder="请输入Code"
        clearable
      ></el-input>
      <el-button
        size="small"
        type="success"
        class="handleSearch"
        icon="el-icon-check"
        @click="pull()"
        >同步配送中心</el-button
      >
    </div>
    <div class="top">
      <el-input
        style="width: 200px; margin-bottom: 5px"
        v-model="search.code"
        placeholder="请输入名称或编码"
        clearable
      ></el-input>
      <el-button
        size="small"
        type="primary"
        class="handleSearch"
        icon="el-icon-search"
        @click="handleSearch()"
        >查询</el-button
      >
      <el-button
        size="small"
        type="danger"
        class="handleSearch"
        icon="el-icon-delete"
        @click="empty()"
        >清空</el-button
      >
      <!-- <el-button
        type="info"
        size="small"
        icon="el-icon-tickets"
        @click="derive()"
        >导出</el-button
      > -->
    </div>
    <el-table
      max-height="600"
      :data="tableData"
      border
      style="width: 100%"
      empty-text="暂无数据"
    >
      <el-table-column
        prop="d_name"
        label="配送中心名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="d_code"
        label="配送中心编码"
        align="center"
      ></el-table-column>
       <el-table-column
        prop="area_code"
        label="区域编码"
        width="180"
        align="center"
      ></el-table-column>
       <el-table-column
        prop="area_name"
        label="区域名称"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column label="仓库" align="center" width="200">
        <template slot-scope="scope">
          <el-select
            style="margin-bottom: 5px"
            v-model="scope.row.warehouse_name"
            :multiple="false"
            filterable
            remote
            center
            reserve-keyword
            placeholder="请选择仓库"
            :remote-method="entrepolistsMethod"
            :loading="entrepot.loading"
            @change="changevalue(scope)"
          >
            <el-option
              v-for="item in entrepot.option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </template>
      </el-table-column>

      <el-table-column
        prop="address"
        label="配送中心地址"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="typeStr"
        label="有效状态"
        align="center"
      ></el-table-column>
      <!-- <el-table-column label="操作" width="300" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            icon="el-icon-info"
            type="info"
            @click="handleinfo(scope)"
            >绑定</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <el-dialog :visible.sync="dialogVisible" center width="300px" @close="cancelbtn">
      <div style="  text-align: center;">是否绑定当前仓库</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelbtn">取 消</el-button>
        <el-button type="primary" @click="sucessbtn">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分页 -->
    <pagination
      @page="handleSizeChange"
      @pagesize="handleCurrentChange"
      :pageData="pageData"
    ></pagination>
    <!-- <warehouseselectDialog
      :dialog="dialog"
      @upData="getData"
    ></warehouseselectDialog> -->
  </div>
</template>
<script>
import Pagination from "@/components/pagination";
import { getToken } from "@/utils/auth";
import { SynchroRDC, SelectList, setWareHouse } from "@/api/select/select";
import { selectChooseWarehouse } from "@/api/warehouse/warehouse";
// import WarehouseselectDialog from "@/components/warehouse/warehouseselectDialog";
export default {
  components: {
    pagination: Pagination,
    // warehouseselectDialog: WarehouseselectDialog,
  },
  data() {
    return {
      //页面数据
      tableData: [],
      tableData1: [],
      rowvalue: {},
      indexs: 0,
      //分页
      pageData: {
        pageSizes: [10, 20],
        pageSize: 10,
        page: 1,
        total: null,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      search: {
        code: "",
        housenamecode: "",
      },
      erpCenter: {
        code: "",
      },
      dialogVisible: false,
      //  entrepolists: "",
      entrepot: {
        innerVisible: false,
        option: [],
        value: "",
        label: "",
        list: [],
        loading: false,
      },
      // dialog: {
      //   centerDialogVisible: false,
      //   id: "",
      // },
    };
  },

  created() {
    //页面数据
    this.getData();
    //取初始商品列表
  },
  methods: {
    //页面数据
    getData() {
      let data = {
        limit: this.pageData.pageSize,
        page: this.pageData.page,
        code_or_name: this.search.code,
      };
      SelectList(data)
        .then((res) => {
          if (res.code == 0) {
            res.data.paginate.data.forEach((res) => {
              res.typeStr = res.is_enable == 1 ? "有效" : "无效";
            });
            this.tableData = res.data.paginate.data;
            this.tableData1 = this.deepClone2(res.data.paginate.data);
            this.pageData.page = res.data.paginate.current_page;
            this.pageData.pagesize = res.data.paginate.per_page;
            this.pageData.total = res.data.paginate.total;
          }
        })
        .catch((err) => {
          console.log(err);
        });
      this.getoptionData();
    },
    deepClone2(obj) {
      var _obj = JSON.stringify(obj),
        objClone = JSON.parse(_obj);
      return objClone;
    },
    // //绑定
    // handleinfo(scope) {
    //   this.dialog.centerDialogVisible = true;
    //   this.dialog.id = scope.row.id;
    // },
    // 本地搜索
    handleSearch() {
      this.pageData.page = 1;
      this.pageData.total = null;
      this.getData();
    },

    changevalue(scope) {
      console.log(scope.row.warehouse_name, 123321);
      this.rowvalue = scope;
      this.dialogVisible = true;
    },
    sucessbtn() {
      this.dialogVisible = false;
      // console.log(
      //   this.rowvalue.row.warehouse_name,
      //   this.rowvalue.row.id,
      //   88888
      // );
      let data = {
        house_id: this.rowvalue.row.warehouse_name,
        id: this.rowvalue.row.id,
      };
      setWareHouse(data)
        .then((res) => {
          if (res.code == 0) {
            this.$message({
              message: res.msg,
              type: "success",
            });
            this.getData()
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
             this.getData()
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    cancelbtn() {
      this.dialogVisible = false;
      // this.getData();
    },
    getoptionData() {
      let data = {
        page: 1,
        limit: 500,
        name: this.search.housenamecode,
      };
      //获取仓库
      selectChooseWarehouse(data).then((res) => {
        if (res.data.data) {
          this.entrepot.option = res.data.data.map((item) => {
            return { value: `${item.id}`, label: `${item.house_name}` };
          });
        }
      });
      this.search.housenamecode = ''
    },

    // 获取仓库方法
    entrepolistsMethod(query) {
      if (query !== "") {
        this.getenterpolistList(query);
      } else {
        this.entrepot.options = [];
      }
    },
    //获取仓库数据
    getenterpolistList(query) {
      this.search.housenamecode = query;
      this.getoptionData();
    },

    //清空
    empty() {
      this.search = {
        code: "",
        housenamecode: "",
      };
      this.erpCenter = {
        code: "",
      };
    },

    //拉取erp
    pull(scope) {
      let data = {
        code: this.erpCenter.code,
      };
      this.$confirm("是否同步配送中心?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          SynchroRDC(data)
            .then((res) => {
              if (res.code == 0) {
                this.search.date = this.erpCenter.date;
                this.getData();
                this.$message({
                  type: "success",
                  message: res.msg,
                });
              }
            })
            .catch((err) => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消同步配送中心",
          });
        });
    },
    derive() {
      console.log(getToken(), 123456);
      this.$confirm("是否导出?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let link = document.createElement("a");
          link.href =
            this.$axios.defaults.baseURL +
            "/smkapi/Difference/exportDifference?token=" +
            getToken() +
            "&code=" +
            this.search.code +
            "&start_date=" +
            this.search.start_time +
            "&end_date=" +
            this.search.end_time;
          link.click();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消导出",
          });
        });
    },
    //每页多少条
    handleCurrentChange(val) {
      this.pageData.page = 1;
      this.pageData.pageSize = val;

      this.getData();
    },
    //选取页数
    handleSizeChange(page) {
      this.pageData.page = page;

      this.getData();
    },
    //选取
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
  },
};
</script>
<style scoped>
.top {
  position: relative;
  margin-bottom: 5px;
}
.btnList {
  position: relative;
  margin-bottom: 5px;
}
.add {
  position: absolute;
  right: 0;
  top: 4px;
}
.pagination {
  text-align: right;
  padding: 10px;
}
</style>